/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisyimport.analysis;

import cz.mzk.kramerius.rukopisyimport.sourceData.TeiDocument;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Rehanek (rehan@mzk.cz)
 */
public class DigitalizationAnalyzer {

    private static final Logger logger = Logger.getLogger(DigitalizationAnalyzer.class.getName());

    /**
     * @param teiRootDir directory containing another subdirectories. Those subdirectories contain
     * actual tei files.
     */
    public AnalyzisResult analyzeRootDir(File teiRootDir, boolean detailedPrint) {
        //TeiStructureDepthStatistics statistics = new TeiStructureDepthStatistics();
        List<File> dirs = Arrays.asList(teiRootDir.listFiles(getDirFilter()));
        AnalyzisResult totalResults = new AnalyzisResult();
        for (File dir : dirs) {
            if (dir.isDirectory()) {
                AnalyzisResult dirResults = analyzeTeiDir(dir);
                totalResults.addFiles(dirResults.getAllDocuments(), dirResults.getDigitalizedDocuments());
            } else {
                logger.log(Level.INFO, "file ''{0}'' is not directory ", dir);
            }
        }
        return totalResults;
    }

    /**
     *
     * @param teiDir directory containing actual tei files
     * @param detailedPrint
     */
    public AnalyzisResult analyzeTeiDir(File dir) {
        AnalyzisResult analysisResult = new AnalyzisResult();
        for (File file : dir.listFiles(getTeiFilter())) {
            try {
                TeiDocument tei = new TeiDocument(file);
                if (tei.isDigitalized()) {
                    analysisResult.addFile(file, true);
                    //digitalized.add(file);
                } else {
                    analysisResult.addFile(file, false);
                    //notDigitalized.add(file);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(DigitalizationAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (DocumentException ex) {
                Logger.getLogger(DigitalizationAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return analysisResult;
    }

    private FilenameFilter getDirFilter() {
        return new FilenameFilter() {
            private static final String svnDirName = ".svn";

            public boolean accept(File dir, String name) {
                return !name.equals(svnDirName);
            }
        };
    }

    private FilenameFilter getTeiFilter() {
        return new FilenameFilter() {
            private static final String suffix = ".tei.xml";

            public boolean accept(File dir, String name) {
                return name.endsWith(suffix);
            }
        };
    }
}
